package taskscheduler.components;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import taskscheduler.components.Scheduler.SchedulerTaskType;
import taskscheduler.components.Table.TableOrderType;

public class Main {

	public static void main(String[] args) throws InterruptedException {
		
		Scheduler scheduler = new Scheduler();
		
		Table table1 = new Table();
		table1.setCouvert(2);
		table1.setName("T3");
		table1.addItem(ItemFactory.nan());
		table1.addItem(ItemFactory.fries());
		table1.addItem(ItemFactory.curry());
		table1.addItem(ItemFactory.rice());
		table1.setOrderType(TableOrderType.ENTREE_PLAT);
		
//		scheduler.addOrder(table);
		
		Table table2 = new Table();
		table2.setName("T4");
		table2.addItem(ItemFactory.nan());
		table2.addItem(ItemFactory.fries());
		table2.addItem(ItemFactory.curry());
		table2.addItem(ItemFactory.rice());
		table1.setOrderType(TableOrderType.ENTREE_PLAT);
		
		TakeAway takeaway = new TakeAway();
		takeaway.setName("E1");
		takeaway.setClient("Fichier");
		takeaway.setClientNumber("0393949393");
		takeaway.addItem(ItemFactory.nan(), 4);
		takeaway.addItem(ItemFactory.fries());
		takeaway.addItem(ItemFactory.curry(), 3);
		takeaway.addItem(ItemFactory.rice());

		TakeAway takeaway2 = new TakeAway();
		takeaway2.setName("E2");
		takeaway2.setClient("Goldman");
		takeaway2.setClientNumber("0393949393");
		takeaway2.addItem(ItemFactory.nan(), 4);
		takeaway2.addItem(ItemFactory.curry(), 3);
		takeaway2.addItem(ItemFactory.rice());

		
		scheduler.command(SchedulerTaskType.ADD_ORDER, takeaway);
		scheduler.command(SchedulerTaskType.ADD_ORDER, takeaway2);
		
		
		printFlux(scheduler);
			

		Thread.currentThread().sleep(1000);
		
		scheduler.command(SchedulerTaskType.ADD_ORDER, table1);
		
		scheduler.command(SchedulerTaskType.FINISH_ORDER);
		
		printFlux(scheduler);
		
		Thread.currentThread().sleep(1000);
		
		scheduler.command(SchedulerTaskType.ADD_ORDER, table2);
		
		scheduler.command(SchedulerTaskType.FINISH_ORDER, table1);
		
		printFlux(scheduler);

	}
	
	private static void printFlux(final Scheduler scheduler) {
		try {
			for (Iterator iterator = scheduler.getFlux().iterator(); iterator.hasNext();) {
				Item item = (Item) iterator.next();
				System.out.println(item.getOrder().getName() + " > " + item.getQuantity() + " " + item.getName() );
			}
		} catch (SchedulerSleepingException e) {
			try {
				Thread.currentThread().sleep(100);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
			printFlux(scheduler);
		}

	}

	
}
